package com.cloud.lan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.lan.api.dto.req.DelReq;
import com.cloud.lan.api.dto.req.LanStoreReq;
import com.cloud.lan.dao.LanStoreMapper;
import com.cloud.lan.entity.base.BaseResponse;
import com.cloud.lan.entity.base.exceptions.BaseException;
import com.cloud.lan.entity.enums.LanError;
import com.cloud.lan.entity.model.dataobject.LanStoreDO;
import com.cloud.lan.entity.utils.VocUtil;
import com.cloud.lan.service.ILanStoreService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

import static com.cloud.lan.entity.utils.ConstantInterface.Enum.ConstantNumber.ONE;
import static com.cloud.lan.entity.utils.ConstantInterface.Enum.ConstantNumber.ZERO;

/**
 * <p>
 * 门店信息表 服务实现类
 * </p>
 *
 * @author xyqierkang@163.com
 * @since 2022-04-28
 */
@Slf4j
@Service
public class LanStoreServiceImpl extends ServiceImpl<LanStoreMapper, LanStoreDO> implements ILanStoreService {

    @Resource
    LanStoreMapper lanStoreMapper;

    @Override
    public BaseResponse manageList(LanStoreReq req) {
        log.info("#查询 门店信息 列表 {}", req);
        LambdaQueryWrapper<LanStoreDO> qw = new LambdaQueryWrapper<>();
        qw.eq(LanStoreDO::getIsDeleted, false).orderByDesc(LanStoreDO::getCreateTime);
        if (StrUtil.isNotEmpty(req.getName())) {
            qw.like(LanStoreDO::getName, req.getName());
        }
        Page<LanStoreDO> page = new Page<>(req.getPageIndex(), req.getPageSize());
        IPage<LanStoreDO> pageList = lanStoreMapper.selectPage(page, qw);
        return BaseResponse.success(pageList);
    }

    @Override
    public BaseResponse add(LanStoreReq req) {
        log.info("#新增 门店信息表 {}", req);
        VocUtil.checkNullAttribute(new String[]{req.getName(), req.getAddress()},
                new String[]{"name", "address"}, LanError.ILLEGAL_PARAMS.getCode());
        LanStoreDO lanStoreDO = new LanStoreDO();
        BeanUtil.copyProperties(req, lanStoreDO);
        LambdaQueryWrapper<LanStoreDO> luwQw = new LambdaQueryWrapper<>();
        luwQw.eq(LanStoreDO::getName, lanStoreDO.getName());
        luwQw.eq(LanStoreDO::getIsDeleted, false);
        if (lanStoreMapper.selectCount(luwQw) > ZERO.getKey()) {
            log.error("{}", LanError.LAN_040001.getMsg());
            throw new BaseException(LanError.LAN_040001);
        }
        lanStoreMapper.insert(lanStoreDO);
        return BaseResponse.success();
    }

    @Override
    public BaseResponse update(LanStoreReq req) {
        log.info("#更新 门店信息表 {}", req);
        VocUtil.checkNullAttribute(new String[]{String.valueOf(req.getId()), req.getName(), req.getAddress()},
                new String[]{"id", "name", "address"}, LanError.ILLEGAL_PARAMS.getCode());
        LanStoreDO lanStoreDO = lanStoreMapper.selectById(req.getId());
        if (ObjectUtil.isEmpty(lanStoreDO)) {
            log.error("{}", LanError.LAN_040002.getMsg());
            throw new BaseException(LanError.LAN_040002);
        }
        BeanUtil.copyProperties(req, lanStoreDO);
        lanStoreDO.setUpdateTime(DateUtil.date());
        lanStoreMapper.updateById(lanStoreDO);
        return BaseResponse.success();
    }

    @Override
    public BaseResponse delete(DelReq req) {
        log.info("#删除 门店信息表 {}", req);
        VocUtil.checkNullAttribute(new String[]{String.valueOf(req.getId())}, new String[]{"id"}, LanError.ILLEGAL_PARAMS.getCode());
        LambdaUpdateWrapper<LanStoreDO> luw = new LambdaUpdateWrapper<>();
        luw.set(LanStoreDO::getIsDeleted, ONE.getKey())
                .set(LanStoreDO::getUpdateTime, DateUtil.date())
                .eq(LanStoreDO::getId, req.getId());
        lanStoreMapper.update(new LanStoreDO(), luw);
        return BaseResponse.success();
    }
}
